package com.qfedu.dao.impl;

import com.qfedu.dao.IProductDao;
import com.qfedu.entity.Product;
import com.qfedu.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ProductDaoImpl implements IProductDao {
    @Override
    public List<Product> listProduct(long typeid, long begin, long pageSize) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select id,typeid,pname,ptime,pimage,pprice,pstar,pinfo from product where typeid=? and isdel=1 limit ?,?",new BeanListHandler<Product>(Product.class),typeid,begin,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Long getAllSize(Integer typeid) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select count(id) from product where typeid = ?",new ScalarHandler<Long>(),typeid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Product getProduct(long id) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select id,typeid,pname,ptime,pimage,pprice,pstar,pinfo from product where id = ?",new BeanHandler<Product>(Product.class),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> queryProductList(String oid) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select pimage,pname,pstar,ptime,pprice,inum as count,imoney as sumPrice from product inner join item on product.id = item.pid where oid = ?",new BeanListHandler<Product>(Product.class),oid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


}
